<?php

/**
 * Session engine class
 * 
 * @package DeadTouch AAC
 * @version 0.0.1
 * @author Kofel <kofels@gmail.com>
 * @copyright 2008 (C) by Kofel
 * @license http://www.gnu.org/licenses/lgpl-3.0.txt GNU Lesser General Public License, Version 3
 * @todo Rewrite
 */
 
class sessionLayer
{
	private $Data = array('lastLogin' => 0, 'sid' => '', 'aid' => 0, 'password' => '', 'action' => '', 'expire' => 1);

	public function isGamemasterAccount()
	{
		$req = intval( Kernel::layer('config')->get('hs_gm_group') );
		
		$a = new OTS_Account;
		$a->load( self::getAccount() );
		
		if( !$a->isLoaded() ) return false;
		
		foreach( $a as $p )
		{
			if( $p->group->id >= $req )
			{
				return true;
			}
		}
		
		return false;
	}
	
	public function load()
	{
		if( !Kernel::layer('database')->pdo )
		{
			throw new KernelExeption('Why connect isn\'t done?');
		}
				
		if( $_COOKIE['dt_cms_sid'] )
		{
			$result = Kernel::layer('database')->query('SELECT `lastLogin`, `aid`, `password`, `action` FROM `cms_session` WHERE `ip` = \''.$this->IPToLong($_SERVER['REMOTE_ADDR']).'\' AND `lastLogin` > \''.(time() - 1300).'\' AND `sid` = \''.mysql_escape_string($_COOKIE['dt_cms_sid']).'\';'); // Maybe work fine?
			if( $result )
			{
				$this->Data = $result->fetch();
				$this->Data['sid'] = $_COOKIE['dt_cms_sid'];
			}
			else
			{
				setcookie('dt_cms_sid', false);
			}
		
		}
	}
	
	public function unload()
	{	
		Kernel::layer('database')->query('DELETE FROM `cms_session` WHERE `cms_session`.`lastLogin` < \''.(time()-1300).'\' AND `cms_session`.`expire` = \'1\';');
		
		if( !$this->Data['aid'] )
		{
			return false;
		}
		$lTime = time();
		
		if( $this->Data['lastLogin'] )
		{
			Kernel::layer('database')->query('UPDATE `cms_session` SET `lastLogin` = \''.$lTime.'\' WHERE `cms_session`.`sid` = \''.$this->Data['sid'].'\';');
		}
		else
		{
			$sid = md5( time().$this->Data['aid'] );
			$ip = $this->IPToLong($_SERVER['REMOTE_ADDR']);
			Kernel::layer('database')->query('INSERT INTO `cms_session` (`ip`, `lastLogin`, `sid`, `aid`, `password`, `action`, `expire`) VALUES (\'' . $ip . '\', \'' . $lTime . '\', \'' . $sid . '\', \'' . $this->Data['aid'] . '\', \'' . $this->Data['password'] . '\', \'' . $this->Data['action'] . '\', \'' . $this->Data['expire'] . '\');');
			setcookie('dt_cms_sid', $sid);
		}
	}
	
	public function destroy()
	{
		Kernel::layer('database')->query('DELETE FROM `cms_session` WHERE `cms_session`.`sid` = \''.$this->Data['sid'].'\';');
		$this->Data = array('lastLogin' => 0, 'sid' => '', 'aid' => 0, 'password' => '', 'action' => '');
	}

	public function getAction()
	{
		return $this->Data['action'];
	}
	
	public function getAccount()
	{
		return $this->Data['aid'];
	}
	
	public function getPassword()
	{
		return $this->Data['password'];
	}
		
	public function setAction( $par )
	{
		$this->Data['action'] = mysql_escape_string($par);
	}
	
	public function setAccount( $par )
	{
		$this->Data['aid'] = intval($par);
	}
	
	public function setPassword( $par )
	{
		$this->Data['password'] = mysql_escape_string($par);
	}
	
	public function setExpire( $par )
	{
		$this->Data['expire'] = intval( $par );
	}
	
	private function IPToLong( $ip )
	{
		if( $ip == '255.255.255.255' )
		{
			return 4294967295;
		}
		else
		{
			return intval(sprintf('%u', ip2long($ip) ));
		}
	}
}
?>